﻿using NUnit.Framework;
using System.IO;
using System;
using USU.MetricsMonitor.Common;

namespace USU.MetricsMonitor.DiffProvider
{
    [TestFixture]
    public class TestDiffProvider
    {
        private static string RepositaryPath;
        private static string DiffLocalDirectory;
        private static string ExpectedRootDiffFile;
        private static Revision TestRev;
        private static string ResultDiffFile;
        private static Stream ActualDiffStream;
        private static Stream ExpectedDiffStream;

        [SetUp]
        public void Init()
        {
            DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
            
            //RepositaryPath = "http://metrics-mon.googlecode.com/svn";
            RepositaryPath = String.Format("file:///{0}/svn-store/TestProject",
                dir.Parent.Parent.Parent.FullName.Replace("\\", "/"));

            DiffLocalDirectory = Directory.GetCurrentDirectory() + @"\RepositDiff\";

            if (!Directory.Exists(DiffLocalDirectory))
            {
                Directory.CreateDirectory(DiffLocalDirectory);
            }

            ExpectedRootDiffFile = Directory.GetCurrentDirectory() + @"\Test\";
            TestRev = new Revision(2);
            ResultDiffFile = DiffLocalDirectory + TestRev + ".diff";

            if (File.Exists(ResultDiffFile))
                File.Delete(ResultDiffFile);
        }

        private void Test_BaseDiffProvider(string ExpectedDiffFile)
        {
            ExpectedDiffStream = File.Open(ExpectedDiffFile, FileMode.Open, FileAccess.Read);
            
            int n = 100;
            byte[] ActualBuffer = new byte[n];
            byte[] ExpectedBuffer = new byte[n];

            while (true)
            {
                int ActualCountOfBytes = ActualDiffStream.Read(ActualBuffer, 0, n);
                int ExpectedCountOfBytes = ExpectedDiffStream.Read(ExpectedBuffer, 0, n);
                Assert.AreEqual(ActualCountOfBytes, ExpectedCountOfBytes);

                if (ActualCountOfBytes == 0)
                    break;

                for (int i = 0; i < ActualCountOfBytes; i++)
                {
                    Assert.AreEqual(ActualBuffer[i], ExpectedBuffer[i]);
                }
            }
        }

        [Test]
        public void Test_SoftecDiffProvider()
        {
            string ExpectedDiffFile = ExpectedRootDiffFile + "SoftecTest.diff";
        	ActualDiffStream = new SoftecDiffProvider(RepositaryPath, DiffLocalDirectory).GetStream(TestRev);
        	Test_BaseDiffProvider(ExpectedDiffFile);
        }

        [Test]
        public void Test_SvnDiffProvider()
        {
            string ExpectedDiffFile = ExpectedRootDiffFile + "SvnTest.diff";
            ActualDiffStream = new SvnDiffProvider(RepositaryPath, DiffLocalDirectory).GetStream(TestRev);
            Test_BaseDiffProvider(ExpectedDiffFile);
        }

        [TearDown]
        public void Down()
        {
            if (ExpectedDiffStream != null)
                ExpectedDiffStream.Close();

            if (ActualDiffStream != null)
                ActualDiffStream.Close();
        }
    }
}
